package algorithm.swordoff;

/**
 * 平衡二叉树
 */

public class SQ55_2 {

    // isBalanced开始的root == null作为递归终点不能丢掉
    public boolean isBalanced(TreeNode root) {
        return root == null || isBalanced(root.left) && isBalanced(root.right) && Math.abs(maxDepth(root.right) - maxDepth(root.left)) <= 1;
    }

    public int maxDepth(TreeNode root) {
        return root == null ? 0 : Math.max(maxDepth(root.left), maxDepth(root.right))+1;
    }

}
